<?php

class Admin_Model_Mark{

	private $mark;
	
	function __construct(){
		$this->mark	= new Admin_Model_DbTable_Diems();
		
	}
	
	public function listMark($where=NULL){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$dbSelect 	= new Zend_Db_Select($dbAdapter);
		
		if($where != NULL){
			return $dbSelect->from(array('d'=>'diem'),'*')
							->joinLeft(array('u'=>'users'), 'u.username = d.username',array(
											'hoDem','ten','email'
										)
							)
							->joinLeft(array('m' => 'monhoc'), 'm.id = d.idMonHoc',array('tenMonHoc'))
							->joinLeft(array('sv' => 'sinhvien'), 'sv.username = d.username',array('idLop'))				
							->where($where);
		}else{
			return $dbSelect->from(array('d'=>'diem'),'*')
							->joinLeft(array('u'=>'users'), 'u.username = d.username',array(
											'hoDem','ten','email'
										)
							)
							->joinLeft(array('sv' => 'sinhvien'), 'sv.username = d.username',array('idLop'))
							->joinLeft(array('m' => 'monhoc'), 'm.id = d.idMonHoc',array('tenMonHoc'));				
							
		}
	}
	
	public function  addMark($username, $idMonHoc, $diem, $heSo=1, $hocKy=1, $namHoc ){

		if($heSo==2){
			$data =array(
				'username'	=>	$username,
				'idMonHoc'	=>	$idMonHoc,
				'diemHeSo2'		=>	$diem,
				'hocKy'			=>	$hocKy,
				'namHoc'		=>	$namHoc
			);
		}elseif ($heSo==3){
			$data =array(
				'username'	=>	$username,
				'idMonHoc'	=>	$idMonHoc,
				'diemThi'		=>	$diem,
				'hocKy'			=>	$hocKy,
				'namHoc'		=>	$namHoc
			);
		}else {
			$data =array(
				'username'		=>	$username,
				'idMonHoc'		=>	$idMonHoc,
				'diemHeSo1'		=>	$diem,
				'hocKy'			=>	$hocKy,
				'namHoc'		=>	$namHoc
			);
		}
		
		$this->mark->insert($data);
	}
	
	public function  updateMark($username, $idMonHoc, $diemHeSo1=null, $diemHeSo2=null, $diemThi =null, $hocKy=1, $namHoc = NULL){
		$data =array(
			'idMonHoc'		=>	$idMonHoc,
			'diemHeSo1'		=>	$diemHeSo1,
			'diemHeSo2'		=>	$diemHeSo2,
			'diemThi'		=>	$diemThi,
			'hocKy'			=>	$hocKy,
			'namHoc'		=>	$namHoc
		);
		
		$this->mark->update($data, "username ='".$username."'");
	}
	
	public function deleteMark($idMon, $heSo, $username){
		$this->mark->delete(
						"idMonHoc = '".$idMon."' and
						heSo = '".$heSo."' and
						username = '".$username."'"
				);
	}
	
	public function update($data,$where){
		$this->mark->update($data, $where);
	}

	public function updateHeSo($username, $idMon, $diemHeSo1,$heSo =1){
		if($heSo == 2){
			$data =array(
				'diemHeSo2'		=>	$diemHeSo1,
			);
		}elseif($heSo ==3){
			$data =array(
				'diemThi'		=>	$diemHeSo1,
			);
		}else{
			$data =array(
				'diemHeSo1'		=>	$diemHeSo1,
			);
		}
		
		$this->mark->update($data, "username = '".$username."' and idMonHoc =".$idMon);
	}
	
	public function fetchRowMark($where=NULL){
		$this->mark->fetchRow($where);
	}
	
	public function fetchALLMark($where=NULL){
		return $this->mark->fetchAll($where);
	}
	
	public function issetUsernameAndMonHoc($username,$idMon,$hocKy, $namHoc){
		$row = $this->mark->fetchRow(
						"username 	= '".$username."' and 
						idMonHoc 	= ".$idMon." and 
						hocKy 		= ".$hocKy. " and 
						namHoc		= ".$namHoc);
		
		if(!empty($row['username'])) return TRUE;
		else return false;
	}
	
	public function issetUsername($username){
		$row = $this->mark->fetchRow("username ='".$username."'");
		
		if(!empty($row['username'])) return TRUE;
		else return false;
	}
	
	public function issetMonHoc($username,$idMon){
		$row = $this->mark->fetchRow("idMonHoc =".$idMon);
		
		if(!empty($row['idMonHoc'])) return TRUE;
		else return false;
	}
	
	

}

